/*
 * $Id: Counters.java 23 2002-09-29 13:59:30Z bpruessmann $
 *
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.internal.core.api;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.sourceforge.p4eclipse.core.api.ClientUser;
import net.sourceforge.p4eclipse.core.api.ICountersResult;
import net.sourceforge.p4eclipse.core.api.IPerforceCommand;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

/**
  * ClientUser object for the 'p4 counters' command.
  * 
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Counters extends ClientUser implements IPerforceCommand
{
	private Result result = new Result();
	private IStatus status;
	
	/**
	  * Convenience method for creating instances of Counters.
	  */
	public static final Counters newInstance()
	{
		MultiStatus status = new MultiStatus(PerforceUIPlugin.ID, IStatus.OK, "", null); //$NON-NLS-1$
		return new Counters(status);
	}
	
	/**
	  * Creates a new instance.
	  */
	private Counters(MultiStatus status)
	{
		super(status);
		this.status = status;
	}
	
	/**
	  * @see IClientUser#outputStat(Map)
	  */
	public void outputStat(Map strDict)
	{
		String key = (String)strDict.get("counter"); //$NON-NLS-1$
		Integer value = Integer.valueOf((String)strDict.get("value")); //$NON-NLS-1$
		
		result.counterValueMap.put(key, value);
	}

	/**
	  * Returns the name of the p4 command of this instance.
	  */
	public String getCommand()
	{
		return "counters"; //$NON-NLS-1$
	}
	
	/**
	  * Returns the result of the operation.
	  */
	public ICountersResult getResult()
	{
		return result;
	}
	
	/**
	  * Returns the IStatus object associated with the Depots instance.
	  */
	public IStatus getStatus()
	{
		return status;
	}
	
	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return true;
	}	

	/**
	  * Implementation of the ICountersResult. Stores information provided
	  * by the 'p4 counters' command.
	  * 
	  * @see ICountersResult
	  */
	private static class Result implements ICountersResult
	{
		private Map counterValueMap = new HashMap();
		
		/**
		  * @see ICountersResult#getCounterValue(String)
		  */
		public Integer getCounterValue(String counterName)
		{
			return (Integer)counterValueMap.get(counterName);
		}

	}
}
